package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    Connection conn=null;
    PreparedStatement prep=null;
    ResultSet rs=null;
    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) throws Exception {
        try {
            List<Food> list=new ArrayList<>();
            conn= DBUtil.getConnection();
            String sql="select * from food where businessId=? order by foodId";
            prep=conn.prepareStatement(sql);
            prep.setInt(1,businessId);
            rs=prep.executeQuery();
            while (rs.next()){
                Food food=new Food();
                food.setBusinessId(rs.getInt("businessId"));
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("remarks"));
                list.add(food);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs,prep);
        }
    }
}
